// pages/activity/turntable/share/share.js
let utilApp = require('../../../../utils/app');
const { regeneratorRuntime } = global


Page({

  /**
   * 页面的初始数据
   */
  data: {
    headerImg:'',
    name:'',
    uid:0
  }, 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      headerImg: options.avatarUrl,
      name: options.name,
      uid: options.uid  
    }) 
   
  },
  help:async function (){
    let that =this;
    let lotteryLoginMsg = wx.getStorageSync('lotteryLoginMsg')
    let addShare = await utilApp.lotteryDoPost('Share/addShare', { uid:that.data.uid,openId: lotteryLoginMsg.openid});
    if(lotteryLoginMsg.uid==that.data.uid){
      utilApp.showModal('失败', '无法为自己帮助', '确定', function () { }, function () { }, false)
    }else{
      if (addShare.data.status == 0){
        utilApp.showModal('失败', addShare.data.msg, '确定', function () { }, function () { }, false)
      }else{
        utilApp.showModal('提示', '帮助好友成功，参与活动可获取大礼包', '立即参与', function () { 
          wx.redirectTo({
            url: '../index/index',
          }) 
        }, function () { }, true)
      }
    }
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function () {
    let that=this;
    let wxGetSetting = utilApp.promisify(wx.getSetting);
    let isUserInfo = await wxGetSetting()
    if (isUserInfo.authSetting['scope.userInfo']) { 
      // 判断是否有储存信息
      if (wx.getStorageSync('lotteryLoginMsg')) {
      } else {
        that.setLoginMsg()
      }
    } else { 
      utilApp.isGetUserInfo('../../../authorization/authorization', '../activity/turntable/share/share?name=' + that.data.name + '&avatarUrl=' + that.data.headerImg + '&uid=' + that.data.uid)
    }
  },
  async setLoginMsg() {
    let json = {};
    let userInfo = wx.getStorageSync('userInfo');
    const wxLogin = utilApp.promisify(wx.login);
    let { code } = await wxLogin();
    let openId = await utilApp.lotteryDoPost('login/getSessionkey', { code });
    json.openid = openId.data.openid;
    let authLogin = await utilApp.lotteryDoPost('login/authLogin', { openId: openId.data.openid, nickName: userInfo.nickName, headUrl: userInfo.avatarUrl });
    console.log(authLogin)
  json.uid = authLogin.data.arr.uid;
    wx.setStorageSync('lotteryLoginMsg', json);
    return json;
  }, 
  /**
   * 生命周期函数--监听页面隐藏
   */
  play(){
    wx.redirectTo({
      url: '../index/index',
    })
  },
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})